{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from pandas import DataFrame,Series\n",
    "import pandas as pd\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "outputs": [],
   "source": [
    "#np.nan是浮点类型，能参与到计算中。但计算的结果总是NaN。\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "outputs": [
    {
     "data": {
      "text/plain": "array([ 1.,  2.,  3., nan,  5.,  6.])"
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "n1 = np.array([1, 2, 3, np.nan, 5, 6])\n",
    "n1"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "outputs": [
    {
     "data": {
      "text/plain": "array([1, 2, 3, None, 5, 6], dtype=object)"
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "n2 = np.array([1, 2, 3, None, 5, 6])\n",
    "n2"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "outputs": [
    {
     "data": {
      "text/plain": "0    1.0\n1    2.0\n2    3.0\n3    NaN\n4    4.0\ndtype: float64"
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s = Series([1,2,3,None,4])\n",
    "s"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "outputs": [
    {
     "data": {
      "text/plain": "nan"
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "n1.sum()\n",
    "n2[3] = np.nan\n",
    "n2.sum()\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "outputs": [
    {
     "data": {
      "text/plain": "17.0"
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.nansum(n1)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "outputs": [
    {
     "data": {
      "text/plain": "    A   B   C   D   E\n0  20   9  96  64  32\n1  87  99  88  75  41\n2  45  98  58  41  90\n3  25  14  29  61   2\n4   2  63  73  65  78",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>A</th>\n      <th>B</th>\n      <th>C</th>\n      <th>D</th>\n      <th>E</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>20</td>\n      <td>9</td>\n      <td>96</td>\n      <td>64</td>\n      <td>32</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>87</td>\n      <td>99</td>\n      <td>88</td>\n      <td>75</td>\n      <td>41</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>45</td>\n      <td>98</td>\n      <td>58</td>\n      <td>41</td>\n      <td>90</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>25</td>\n      <td>14</td>\n      <td>29</td>\n      <td>61</td>\n      <td>2</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>2</td>\n      <td>63</td>\n      <td>73</td>\n      <td>65</td>\n      <td>78</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = np.random.randint(0 ,100, size=(5, 5))\n",
    "df = pd.DataFrame(data=data, columns=list('ABCDE'))\n",
    "\n",
    "df\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "outputs": [
    {
     "data": {
      "text/plain": "    A     B     C   D   E\n0  20   9.0  96.0  64  32\n1  87  99.0  88.0  75  41\n2  45   NaN   NaN  41  90\n3  25  14.0  29.0  61   2\n4   2  63.0  73.0  65  78",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>A</th>\n      <th>B</th>\n      <th>C</th>\n      <th>D</th>\n      <th>E</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>20</td>\n      <td>9.0</td>\n      <td>96.0</td>\n      <td>64</td>\n      <td>32</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>87</td>\n      <td>99.0</td>\n      <td>88.0</td>\n      <td>75</td>\n      <td>41</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>45</td>\n      <td>NaN</td>\n      <td>NaN</td>\n      <td>41</td>\n      <td>90</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>25</td>\n      <td>14.0</td>\n      <td>29.0</td>\n      <td>61</td>\n      <td>2</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>2</td>\n      <td>63.0</td>\n      <td>73.0</td>\n      <td>65</td>\n      <td>78</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.loc[2,\"B\"] = np.nan\n",
    "df.loc[2,\"C\"] = None\n",
    "df"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "outputs": [
    {
     "data": {
      "text/plain": "       A      B      C      D      E\n0  False  False  False  False  False\n1  False  False  False  False  False\n2  False   True   True  False  False\n3  False  False  False  False  False\n4  False  False  False  False  False",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>A</th>\n      <th>B</th>\n      <th>C</th>\n      <th>D</th>\n      <th>E</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>False</td>\n      <td>False</td>\n      <td>False</td>\n      <td>False</td>\n      <td>False</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>False</td>\n      <td>False</td>\n      <td>False</td>\n      <td>False</td>\n      <td>False</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>False</td>\n      <td>True</td>\n      <td>True</td>\n      <td>False</td>\n      <td>False</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>False</td>\n      <td>False</td>\n      <td>False</td>\n      <td>False</td>\n      <td>False</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>False</td>\n      <td>False</td>\n      <td>False</td>\n      <td>False</td>\n      <td>False</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.isnull()"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "outputs": [
    {
     "data": {
      "text/plain": "      A      B      C     D     E\n0  True   True   True  True  True\n1  True   True   True  True  True\n2  True  False  False  True  True\n3  True   True   True  True  True\n4  True   True   True  True  True",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>A</th>\n      <th>B</th>\n      <th>C</th>\n      <th>D</th>\n      <th>E</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>True</td>\n      <td>True</td>\n      <td>True</td>\n      <td>True</td>\n      <td>True</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>True</td>\n      <td>True</td>\n      <td>True</td>\n      <td>True</td>\n      <td>True</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>True</td>\n      <td>False</td>\n      <td>False</td>\n      <td>True</td>\n      <td>True</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>True</td>\n      <td>True</td>\n      <td>True</td>\n      <td>True</td>\n      <td>True</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>True</td>\n      <td>True</td>\n      <td>True</td>\n      <td>True</td>\n      <td>True</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.notnull()"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "outputs": [
    {
     "data": {
      "text/plain": "A    False\nB    False\nC    False\nD    False\nE    False\ndtype: bool"
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# all(): 必须全部为True才为True， 类似and\n",
    "# any(): 只要有一个为True即为True,  类似or\n",
    "df.isnull().all()"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "outputs": [
    {
     "data": {
      "text/plain": "A    False\nB     True\nC     True\nD    False\nE    False\ndtype: bool"
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.isnull().any()"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "outputs": [
    {
     "data": {
      "text/plain": "0    False\n1    False\n2     True\n3    False\n4    False\ndtype: bool"
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.isnull().any(axis=1)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "outputs": [
    {
     "data": {
      "text/plain": "A    False\nB     True\nC     True\nD    False\nE    False\ndtype: bool"
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.isnull().any(axis=0)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "outputs": [
    {
     "data": {
      "text/plain": "    A   D   E\n0  77  88  46\n1  38  37  82\n2  53  76  94\n3  42  70  51\n4  45  36  24",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>A</th>\n      <th>D</th>\n      <th>E</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>77</td>\n      <td>88</td>\n      <td>46</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>38</td>\n      <td>37</td>\n      <td>82</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>53</td>\n      <td>76</td>\n      <td>94</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>42</td>\n      <td>70</td>\n      <td>51</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>45</td>\n      <td>36</td>\n      <td>24</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.dropna() # 默认删除空值的行\n",
    "df.dropna(axis=1)#删除空值的列\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "outputs": [
    {
     "data": {
      "text/plain": "    A     B     C   D   E\n0  77   2.0  13.0  88  46\n1  38  40.0  57.0  37  82\n2  53  19.5  35.5  76  94\n3  42  13.0  43.0  70  51\n4  45  23.0  29.0  36  24",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>A</th>\n      <th>B</th>\n      <th>C</th>\n      <th>D</th>\n      <th>E</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>77</td>\n      <td>2.0</td>\n      <td>13.0</td>\n      <td>88</td>\n      <td>46</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>38</td>\n      <td>40.0</td>\n      <td>57.0</td>\n      <td>37</td>\n      <td>82</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>53</td>\n      <td>19.5</td>\n      <td>35.5</td>\n      <td>76</td>\n      <td>94</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>42</td>\n      <td>13.0</td>\n      <td>43.0</td>\n      <td>70</td>\n      <td>51</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>45</td>\n      <td>23.0</td>\n      <td>29.0</td>\n      <td>36</td>\n      <td>24</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.fillna(10)\n",
    "df[\"B\"].fillna(df[\"B\"].mean())\n",
    "df[\"B\"] = df[\"B\"].fillna(df[\"B\"].mean())\n",
    "df[\"C\"] = df[\"C\"].fillna(df[\"C\"].mean())\n",
    "df"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "outputs": [],
   "source": [
    "cond = df.isnull().any(axis=1)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "outputs": [
    {
     "data": {
      "text/plain": "    A     B     C   D   E\n0  77   2.0  13.0  88  46\n1  38  40.0  57.0  37  82\n3  42  13.0  43.0  70  51\n4  45  23.0  29.0  36  24",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>A</th>\n      <th>B</th>\n      <th>C</th>\n      <th>D</th>\n      <th>E</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>77</td>\n      <td>2.0</td>\n      <td>13.0</td>\n      <td>88</td>\n      <td>46</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>38</td>\n      <td>40.0</td>\n      <td>57.0</td>\n      <td>37</td>\n      <td>82</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>42</td>\n      <td>13.0</td>\n      <td>43.0</td>\n      <td>70</td>\n      <td>51</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>45</td>\n      <td>23.0</td>\n      <td>29.0</td>\n      <td>36</td>\n      <td>24</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df[~cond]\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "outputs": [
    {
     "data": {
      "text/plain": "    A   D   E\n0  77  88  46\n1  38  37  82\n2  53  76  94\n3  42  70  51\n4  45  36  24",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>A</th>\n      <th>D</th>\n      <th>E</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>77</td>\n      <td>88</td>\n      <td>46</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>38</td>\n      <td>37</td>\n      <td>82</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>53</td>\n      <td>76</td>\n      <td>94</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>42</td>\n      <td>70</td>\n      <td>51</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>45</td>\n      <td>36</td>\n      <td>24</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cond1 = df.notnull().all()\n",
    "df.loc[:,cond1]"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "outputs": [],
   "source": [
    "df.dropna(axis=1,inplace=True)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "outputs": [
    {
     "data": {
      "text/plain": "    A     B     C   D   E\n0  20   9.0  96.0  64  32\n1  87  99.0  88.0  75  41\n2  45   NaN   NaN  41  90\n3  25  14.0  29.0  61   2\n4   2  63.0  73.0  65  78",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>A</th>\n      <th>B</th>\n      <th>C</th>\n      <th>D</th>\n      <th>E</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>20</td>\n      <td>9.0</td>\n      <td>96.0</td>\n      <td>64</td>\n      <td>32</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>87</td>\n      <td>99.0</td>\n      <td>88.0</td>\n      <td>75</td>\n      <td>41</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>45</td>\n      <td>NaN</td>\n      <td>NaN</td>\n      <td>41</td>\n      <td>90</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>25</td>\n      <td>14.0</td>\n      <td>29.0</td>\n      <td>61</td>\n      <td>2</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>2</td>\n      <td>63.0</td>\n      <td>73.0</td>\n      <td>65</td>\n      <td>78</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "outputs": [
    {
     "data": {
      "text/plain": "    A      B      C   D   E\n0  20    9.0   96.0  64  32\n1  87   99.0   88.0  75  41\n2  45  100.0  100.0  41  90\n3  25   14.0   29.0  61   2\n4   2   63.0   73.0  65  78",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>A</th>\n      <th>B</th>\n      <th>C</th>\n      <th>D</th>\n      <th>E</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>20</td>\n      <td>9.0</td>\n      <td>96.0</td>\n      <td>64</td>\n      <td>32</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>87</td>\n      <td>99.0</td>\n      <td>88.0</td>\n      <td>75</td>\n      <td>41</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>45</td>\n      <td>100.0</td>\n      <td>100.0</td>\n      <td>41</td>\n      <td>90</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>25</td>\n      <td>14.0</td>\n      <td>29.0</td>\n      <td>61</td>\n      <td>2</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>2</td>\n      <td>63.0</td>\n      <td>73.0</td>\n      <td>65</td>\n      <td>78</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.fillna(100)\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "outputs": [
    {
     "data": {
      "text/plain": "      A     B     C     D     E\n0  20.0   9.0  96.0  64.0  32.0\n1  87.0  99.0  88.0  75.0  41.0\n2  45.0  41.0  41.0  41.0  90.0\n3  25.0  14.0  29.0  61.0   2.0\n4   2.0  63.0  73.0  65.0  78.0",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>A</th>\n      <th>B</th>\n      <th>C</th>\n      <th>D</th>\n      <th>E</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>20.0</td>\n      <td>9.0</td>\n      <td>96.0</td>\n      <td>64.0</td>\n      <td>32.0</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>87.0</td>\n      <td>99.0</td>\n      <td>88.0</td>\n      <td>75.0</td>\n      <td>41.0</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>45.0</td>\n      <td>41.0</td>\n      <td>41.0</td>\n      <td>41.0</td>\n      <td>90.0</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>25.0</td>\n      <td>14.0</td>\n      <td>29.0</td>\n      <td>61.0</td>\n      <td>2.0</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>2.0</td>\n      <td>63.0</td>\n      <td>73.0</td>\n      <td>65.0</td>\n      <td>78.0</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df\n",
    "df.fillna(method=\"ffill\") #上面的数据填充自己\n",
    "df.fillna(method=\"bfill\") #下面的数据填充自己\n",
    "df.fillna(method=\"ffill\",axis=1) #左面的数据填充自己\n",
    "df.fillna(method=\"bfill\",axis=1) #右面的数据填充自己"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "outputs": [
    {
     "data": {
      "text/plain": "    A     B     C   D   E\n0  20   9.0  96.0  64  32\n1  87  99.0  88.0  75  41\n2  45   NaN   NaN  41  90\n3  25  14.0  29.0  61   2\n4   2  63.0  73.0  65  78",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>A</th>\n      <th>B</th>\n      <th>C</th>\n      <th>D</th>\n      <th>E</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>20</td>\n      <td>9.0</td>\n      <td>96.0</td>\n      <td>64</td>\n      <td>32</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>87</td>\n      <td>99.0</td>\n      <td>88.0</td>\n      <td>75</td>\n      <td>41</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>45</td>\n      <td>NaN</td>\n      <td>NaN</td>\n      <td>41</td>\n      <td>90</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>25</td>\n      <td>14.0</td>\n      <td>29.0</td>\n      <td>61</td>\n      <td>2</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>2</td>\n      <td>63.0</td>\n      <td>73.0</td>\n      <td>65</td>\n      <td>78</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "outputs": [
    {
     "data": {
      "text/plain": "    A     B     C   D   E\n0  20   9.0  96.0  64  32\n1  45   NaN   NaN  41  90\n2  45   NaN   NaN  41  90\n3  25  14.0  29.0  61   2\n4   2  63.0  73.0  65  78",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>A</th>\n      <th>B</th>\n      <th>C</th>\n      <th>D</th>\n      <th>E</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>20</td>\n      <td>9.0</td>\n      <td>96.0</td>\n      <td>64</td>\n      <td>32</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>45</td>\n      <td>NaN</td>\n      <td>NaN</td>\n      <td>41</td>\n      <td>90</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>45</td>\n      <td>NaN</td>\n      <td>NaN</td>\n      <td>41</td>\n      <td>90</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>25</td>\n      <td>14.0</td>\n      <td>29.0</td>\n      <td>61</td>\n      <td>2</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>2</td>\n      <td>63.0</td>\n      <td>73.0</td>\n      <td>65</td>\n      <td>78</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.loc[1] = df.loc[2]\n",
    "df"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "outputs": [
    {
     "data": {
      "text/plain": "    A     B     C   D   E\n0  20   9.0  96.0  64  32\n1  45   NaN   NaN  41  90\n3  25  14.0  29.0  61   2\n4   2  63.0  73.0  65  78",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>A</th>\n      <th>B</th>\n      <th>C</th>\n      <th>D</th>\n      <th>E</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>20</td>\n      <td>9.0</td>\n      <td>96.0</td>\n      <td>64</td>\n      <td>32</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>45</td>\n      <td>NaN</td>\n      <td>NaN</td>\n      <td>41</td>\n      <td>90</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>25</td>\n      <td>14.0</td>\n      <td>29.0</td>\n      <td>61</td>\n      <td>2</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>2</td>\n      <td>63.0</td>\n      <td>73.0</td>\n      <td>65</td>\n      <td>78</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.drop_duplicates()"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "outputs": [
    {
     "data": {
      "text/plain": "    A     B     C   D   E\n0  20   9.0  96.0  64  32\n3  25  14.0  29.0  61   2\n4   2  63.0  73.0  65  78",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>A</th>\n      <th>B</th>\n      <th>C</th>\n      <th>D</th>\n      <th>E</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>20</td>\n      <td>9.0</td>\n      <td>96.0</td>\n      <td>64</td>\n      <td>32</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>25</td>\n      <td>14.0</td>\n      <td>29.0</td>\n      <td>61</td>\n      <td>2</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>2</td>\n      <td>63.0</td>\n      <td>73.0</td>\n      <td>65</td>\n      <td>78</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 67,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# df.drop_duplicates(keep=\"first\")\n",
    "# df.drop_duplicates(keep=\"last\")\n",
    "df.drop_duplicates(keep=False) # 两个都不留"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "outputs": [
    {
     "data": {
      "text/plain": "    A     B     C   D   E  (4, A)\n0  20   9.0  96.0  64  32      25\n1  45   NaN   NaN  41  90      25\n2  45   NaN   NaN  41  90      25\n3  25  14.0  29.0  61   2      25\n4  25  63.0  73.0  65  78      25",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>A</th>\n      <th>B</th>\n      <th>C</th>\n      <th>D</th>\n      <th>E</th>\n      <th>(4, A)</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>20</td>\n      <td>9.0</td>\n      <td>96.0</td>\n      <td>64</td>\n      <td>32</td>\n      <td>25</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>45</td>\n      <td>NaN</td>\n      <td>NaN</td>\n      <td>41</td>\n      <td>90</td>\n      <td>25</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>45</td>\n      <td>NaN</td>\n      <td>NaN</td>\n      <td>41</td>\n      <td>90</td>\n      <td>25</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>25</td>\n      <td>14.0</td>\n      <td>29.0</td>\n      <td>61</td>\n      <td>2</td>\n      <td>25</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>25</td>\n      <td>63.0</td>\n      <td>73.0</td>\n      <td>65</td>\n      <td>78</td>\n      <td>25</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 72,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.loc[4,\"A\"] = 25\n",
    "df\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "outputs": [
    {
     "data": {
      "text/plain": "    A     B     C   D   E  (4, A)\n0  20   9.0  96.0  64  32      25\n1  45   NaN   NaN  41  90      25\n3  25  14.0  29.0  61   2      25",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>A</th>\n      <th>B</th>\n      <th>C</th>\n      <th>D</th>\n      <th>E</th>\n      <th>(4, A)</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>20</td>\n      <td>9.0</td>\n      <td>96.0</td>\n      <td>64</td>\n      <td>32</td>\n      <td>25</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>45</td>\n      <td>NaN</td>\n      <td>NaN</td>\n      <td>41</td>\n      <td>90</td>\n      <td>25</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>25</td>\n      <td>14.0</td>\n      <td>29.0</td>\n      <td>61</td>\n      <td>2</td>\n      <td>25</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 73,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.drop_duplicates(subset=[\"A\"])"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "outputs": [
    {
     "data": {
      "text/plain": "0    False\n1    False\n2     True\n3    False\n4     True\ndtype: bool"
     },
     "execution_count": 75,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.duplicated(subset=[\"A\"])"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}